/*
** stockPrices.c for getStockPrices in /home/comet_a//afs/local/bourseProject
** 
** Made by alexandre comet
** Login   <comet_a@epitech.net>
** 
** Started on  Fri Nov 23 22:34:12 2012 alexandre comet
** Last update Thu Nov 29 15:34:26 2012 Ralph
*/

#include	<stdio.h>
#include	<stdlib.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<fcntl.h>
#include	<string.h>
#include	"my.h"

char		*downloadPage(char *indice)
{
  if (strcmp(indice, "sbf250") == 0) {
    system("wget http://bourse.trader-finance.fr/cours-bourse/actions-france.html");
    return ("actions-france.html");
  }
  else if (strcmp(indice, "cac40") == 0) {
    system("wget http://bourse.trader-finance.fr/cours-bourse/action-cac-40.html");
    return ("action-cac-40.html");
  }
  else {
    printf("L'indice demande est invalide.\n");
    exit(1);
  }
}

void		deleteHtml(char *fileName)
{
  char		*cmd = xmalloc((strlen(fileName) + 6) * sizeof(*cmd));

  cmd = NULL;
  strcat(cmd, "rm ");
  strcat(cmd, fileName);
  system(cmd);
  free(cmd);
}

char		*mySubStr(char *src, int pos, int len)
{
  char		*dest;

  dest = NULL;
  if (len > 0)
    {
      dest = calloc(len + 1, 1);
      if (dest != NULL)
	strncat(dest, src+pos, len);
    }
  return (dest);
}

int		isActionLine(char *line)
{
  if (strstr(line, "<td") != NULL && strstr(line, "title=") != NULL)
    return 0;
  return -1;
}

char		*epurLine(char *line)
{
  int		i = 0, j = 0;

  char		*new = xmalloc((strlen(line) + 2) * sizeof(*new));
  while (line[i])
    {
      while ((line[i] && (line[i] == ' ')) || (line[i] == '\t'))
	i++;
      while (line[i])
	new[j++] = line[i++];
      new[j] = '\0';
    }
  return new;
}

int		isMaj(char c)
{
  if (c >= 'A' && c <= 'Z')
    return 0;
  return -1;
}

int		isNum(char c)
{
  if ((c >= '0' && c <= '9') || (c == '.'))
    return 0;
  return -1;
}

void		stockPrices(char *file)
{
  int		fd;

  fd = open(file, O_RDONLY);
  get_to_wordtab(fd);
  close(fd);
  deleteHtml(file);
}
